package com.lonton.service.impl;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.lonton.dao.EmployeeDao;
import com.lonton.dao.UserDao;
import com.lonton.domain.Employee;
import com.lonton.service.EmployeeService;
@Service
public class EmployeeServiceImpl implements EmployeeService {
	@Autowired
	private EmployeeDao employeeDao;

	@Autowired
	private UserDao userDao;
	@Autowired
	private RedisTemplate<Object,Object> redisTemplate;

	public void insertEmp(Employee employee) {
		employeeDao.insertEmp(employee);
	}

	@SuppressWarnings("unchecked")
	public List<Employee> findAll() {
		BoundValueOperations<Object, Object> ops= redisTemplate.boundValueOps("userList");
		List<Employee> userList = (List<Employee>) ops.get();
		if (ops.get()!=null) {
			System.out.println(ops.get().toString());
		}

		if (userList==null||userList.size()<1) {
			synchronized (this) {
				userList = (List<Employee>) ops.get();
				if (userList==null||userList.size()<1) {
					userList = userDao.findAll();
					ops.set(userList, 10, TimeUnit.SECONDS);
				}
			}
		}
		return	userList;
	}
}
